package com.zwj.controller;

import com.zwj.dto.Result;
import com.zwj.service.FileService;
import io.swagger.annotations.ApiOperation;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;


@Controller
@RequestMapping("/file")
public class FileController {
    private final FileService fileService;
    public FileController(FileService fileService) {
        this.fileService = fileService;
    }

    @ResponseBody
    //单附件上传
    @ApiOperation(value = "上传头像 新", notes = "上传头像 新")
    @PostMapping(value = "/upload", headers = "content-type=multipart/form-data")
    public Result upload(@RequestPart MultipartFile file) {
        return fileService.upload(file);
    }
    @ApiOperation(value = "下载附件 新", notes = "下载附件 新")
    @GetMapping("/download/{fileName:.+}")
    @ResponseBody
    public ResponseEntity<Resource> downloadFile(@PathVariable String fileName, HttpServletRequest request) throws UnsupportedEncodingException {
        return fileService.downloadFile(fileName, request);
    }
}
